// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

// let userId = "1354255569179971599";
let userId = "";
var search_value = '';

let Authorization = "";
let refreshToken = "";
// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    getHuntingStatus(currentPage, pageSize, search_value);
}

function getHuntingStatus(currentPage, pageSize, search_value) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/huntingInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            userId: userId
        },
        success: function (data) {
            console.log("所有的岗位申请状态数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#huntingList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let status = "";
                if (item.huntingStatus === "邀面试") {
                    status = "<span class='rejected'>邀面试</span>";
                } else if (item.huntingStatus === "感兴趣") {
                    status = "<span class='approved'>感兴趣</span>";
                } else {
                    status = "<span class='pending'>" + item.huntingStatus + "</span>";
                }

                let del = "";
                if (item.huntingStatus === "不感兴趣") {
                    del = "<a class='view-application' onclick='updateStatus(\"" + item.huntingId + "\", \"" + item.deleted + "\")'>删除</a>";
                } else {
                    del = "<span class='view-application'>暂不可删</span>";
                }

                $("#huntingList").append(
                    "<tr class='application-item'>" +
                    "<td class='application-job'>" +
                    "<h3><a onclick='jobDetail(\""+item.jobId+"\", \""+item.companyId+"\")'>" + item.jobName + "</a></h3>" +
                    "</td>" +

                    "<td class='application-employer'>" +
                    "<a class='dotted'>" + item.companyName + "</a>" +
                    "</td>" +

                    "<td class='status'>" +
                    status +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span> " + item.createDate + " </span>" +
                    "</td>" +

                    "<td class='download-cv text-xl-right'>" +
                    del +
                    "</td>" +
                    "</tr>"
                );
            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getHuntingStatus(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

//  搜索框
function click_search() {
    var tmp = $('#hunting_search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;
    getHuntingStatus(1, pageSize, search_value);
}

function updateStatus(huntingId, deleted) {
    $.confirm({
        title: '提示',
        content: '确定删除当前记录吗？<br/>',
        type: 'green',
        theme: "modern",
        closeAnimation: 'scaleY',
        buttons: {
            ok: {
                text: '确认',
                btnClass: 'btn-warning',
                action: function () {
                    let status = $("#status").selected().val();
                    $.ajax({
                        type: "DELETE",
                        async: false,
                        url: jobsServer + "/jobs/huntingInfo?huntingId=" + huntingId + "&deleted=" + deleted,
                        headers: {
                            "Authorization": Authorization,
                            "refreshToken": refreshToken,
                        },
                        success: function (data) {
                            if (data.resp_code === 0) {
                                // 成功
                                location.reload();
                            } else {
                                // 失败
                                cocoMessage.error(2000, data.resp_msg, function () {
                                    console.log(data.resp_msg);
                                });
                            }
                        },
                        error: function (err) {
                            // 异常
                            cocoMessage.error(2000, err.statusText, function () {
                                console.log(err);
                            });
                        }
                    })
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-default'
            }
        }
    });
}
function jobDetail(jobId, companyId){
    location.href ="/oa/jobs/jobDetails/"+jobId+"/"+companyId;
}
